Release 10.1A: OpenEdge Development:
Progress 4GL Handbook


Using the AppBuilder to generate function definitions

If you want to build a procedure file of any size with a number of internal entries, whether internal procedures or functions, you should definitely use the AppBuilder to create it for you. The AppBuilder generates most of the supporting statements you’ve just read about here for user-defined functions, and provides a separate code section for each to make it easy to maintain your procedures.

To see how to define a function in the AppBuilder and what it does for you:

  1. Go into the Section Editor for the new h-OrderWin.w procedure.
  2. Select Functions from the Section drop-down list. This message appears:
  3. Answer Yes. The New Function dialog box appears:
  4. Enter the function name dataColor and specify INTEGER for the Returns value, then click OK.
  5. Enter this definition for the function:
  6. RETURNS INTEGER 
      ( daFirst AS DATE, daSecond AS DATE ) : 
    /*--------------------------------------------------------------------- 
      Purpose:  Provides a standard warning color if one date  
                is too far from another. 
        Notes:  The function returns a color code of: 
                -- yellow if the dates differ at all 
                -- purple if they are more than five days apart 
                -- red if they are more than ten days apart 
    ---------------------------------------------------------------------*/ 
      DEFINE VARIABLE iDifference AS INTEGER    NO-UNDO. 
      iDifference = daSecond - daFirst. 
      RETURN IF iDifference = ? OR iDifference > 10 THEN 12 /* Red */ 
             ELSE IF iDifference > 5 THEN 13 /* Purple */ 
             ELSE IF iDifference > 0 THEN 14 /* Yellow */ 
             ELSE ?.  /* Unknown value keeps the default background color. */ 
    END FUNCTION. 
    

    The AppBuilder has generated the FUNCTION dataColor header syntax along with the RETURNS phrase, a placeholder RETURN statement and the END FUNCTION statement. You fill in the parameter definitions where the comment prompts you to do that. The function accepts two dates as INPUT parameters, calculates the difference between them (which is an integer value), and returns an integer value representing the appropriate background color for the second date. This is color code 12, 13, or 14, representing colors red, purple, and yellow, depending on how far apart the two dates are and whether either of them is undefined.

  7. Go into the main block and add a line of code to invoke the function and assign the BGCOLOR attribute for background color to the date field when the row is displayed:
  8. MAIN-BLOCK: 
    DO ON ERROR   UNDO MAIN-BLOCK, LEAVE MAIN-BLOCK 
       ON END-KEY UNDO MAIN-BLOCK, LEAVE MAIN-BLOCK: 
      RUN enable_UI. 
      shipDate:BGCOLOR = dateColor(PromiseDate, ShipDate). 
      IF NOT THIS-PROCEDURE:PERSISTENT THEN 
        WAIT-FOR CLOSE OF THIS-PROCEDURE. 
    END. 
    

  9. Save h-OrderWin.w, then Run h-CustOrderWin6.w.
  10. There are only a few Orders where the ShipDate is later than the PromiseDate.

  11. To find a few, enter a New State of GA for Georgia.
  12. Click the Next button to get to Customer 3.
  13. Select Order number 160 in the browse and click the Order Detail button to run h-OrderWin.w. You see the field is displayed in red, as it should be:
  14. Select Order number 189, which has no ShipDate at all, and click Order Detail:
  15. The ShipDate shows up in red here also because it has the Unknown value (?).

    Think about the code you just wrote. You defined an implementation for a function called dateColor, which goes to the bottom of the h-OrderWin.w procedure file, along with any internal procedures. Then you added a line of code to the main block that references the function.

    The main block is above the function in the procedure, so why didn’t Progress complain that there was no declaration for the function? The reason is that the AppBuilder generated it for you.

  16. Go into the Compile Code Preview window for h-OrderWin.w and search for dateColor (use the CTRL+F sequence for Find).

You’ll see the function prototype in one of the header sections that comes before the main block or any other executable code:

/* ************************  Function Prototypes ********************** */ 
FUNCTION dateColor RETURNS INTEGER 
   ( daFirst AS DATE, daSecond AS DATE )   FORWARD. 

Thus, the AppBuilder not only helps you generate the header for the function code itself, but it also generates a prototype for you and organizes all the code into sections where you can easily inspect and maintain individual entries.

The AppBuilder copies the parameter definitions from your function header just as you entered it in the Section Editor so you cannot remove them from the function header, even though in a hand-coded procedure file you could define them in the prototype and leave them out of the function header itself.


Copyright © 2005 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095